﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tool;

namespace MyMusicList.DBModelValidate
{
    /// <summary>
    /// 存放要验证的字段信息
    /// </summary>
    public class ModelRule
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string name { get; set; }

        /// <summary>
        /// 字段的值
        /// </summary>
        public object value { get; set; }
    }

    /// <summary>
    /// 给与要验证的字段的验证规则
    /// </summary>
    public class ModelRuleValidate
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string name { get; set; }
        public int? StringLenth { set; get; }
        //是否必填
        public bool Require { set; get; }

        /// <summary>
        /// 各种判定名称
        /// </summary>
        public string RuleName { set; get; }
        /// <summary>
        ///  各种判定值
        /// </summary>
        public object RuleVlues { set; get; }
        public string ErrorMessge { set; get; }
    }


    public class ModelRuleValidateR : Init_Output_Info
    {
        //存储规则
        public static string[] rule;
        //存储错误信息
        public static string[] message;

        //存储规则与信息的返回
        public static List<ModelRuleValidate> modelRuleValidateList = new List<ModelRuleValidate>();

        //public static 

        public void AddParas()
        {
            SetmodelRuleValidateList(rule, message);
        }


        public static void SetmodelRuleValidateList(string[] rule, string[] message)
        {
            ModelRuleValidate modelRuleValidate = new ModelRuleValidate();
            for (int i = 0; i < rule.Length; i++)
            {
                for (int z = 0; z < rule[i].Split("=>")[1].Split("|").Length; z++)
                {
                    modelRuleValidate = new ModelRuleValidate();
                    modelRuleValidate.name = rule[i].Split("=>")[0].ToString();
                    modelRuleValidate.RuleName = rule[i].Split("=>")[1].Split("|")[z].ToString().Split(":")[0];

                   
                    if (rule[i].Split("=>")[1].Split("|")[z].IndexOf(":")>-1)
                    { 
                        if(rule[i].Split("=>")[1].Split("|")[z].Split(":")[0].ToString()== "Require")
                        {
                            modelRuleValidate.RuleVlues = true;
                        }
                        else
                        {
                            modelRuleValidate.RuleVlues = rule[i].Split("=>")[1].Split("|")[z].ToString().Split(":")[1];
                        }
                       
                    }

                   

                    string[] output =
                    message.Select(x => x.Contains(modelRuleValidate.name + "." + modelRuleValidate.RuleName) ?
                    x:null).ToArray();

                    if (output.Length != 0)
                    {
                        modelRuleValidate.ErrorMessge = output.Where(x=>x!=null).ToArray()[0].Split("=>")[1].ToString();
                    }
                    modelRuleValidateList.Add(modelRuleValidate);
                }

            }
        }

        //默认先执行
        public ModelRuleValidateR()
        {
            modelRuleValidateList = new List<ModelRuleValidate>();
            rule = new string[] { };
            message = new string[] { };
        }

        public  bool getIsOK<T>(T t)
        {
            AddParas();
            DoValidate<T> doValidate = new DoValidate<T>();
            return doValidate.DoVa(t);
        }

        public static void ModelRuleValidateRC(string ModelRule, string ErrorMessge)
        {
            ModelRuleValidate modelRuleValidate = new ModelRuleValidate();
            var ModelRuleArray = ModelRule.Split('.');
            modelRuleValidate.name = ModelRuleArray[0];

            //判断长度
            if (ModelRuleArray[1].IndexOf("StringLenth") > -1)
            {
                modelRuleValidate.StringLenth = Convert.ToInt32(ModelRuleArray[1].Split(':')[1]);
                modelRuleValidate.ErrorMessge = ErrorMessge;
                modelRuleValidateList.Add(modelRuleValidate);
            }
            if (ModelRuleArray[1].IndexOf("Require") > -1)
            {
                modelRuleValidate.Require = true;
                modelRuleValidate.ErrorMessge = ErrorMessge;
                modelRuleValidateList.Add(modelRuleValidate);
            }
        }

    }

    public class ModelRuleZ
    {
        /// <summary>
        /// 存放数据
        /// </summary>
        public List<ModelRule> modelRulesList { set; get; }
        /// <summary>
        /// 存放规则
        /// </summary>
        public List<ModelRuleValidate> modelRuleValidateList { set; get; }

        public ModelRuleZ(List<ModelRuleValidate> modelRuleValidateList)
        {
            this.modelRuleValidateList = modelRuleValidateList;
        }
    }

    /// <summary>
    /// 获取类的名字与值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class getModelRule<T>
    {
        public static List<ModelRule> modelRuleList = new List<ModelRule>();

        public getModelRule(T t)
        {
            modelRuleList = new List<ModelRule>();
            ModelRule mr;
            foreach (var ca in t.GetType().GetProperties().ToList())
            {
                mr = new ModelRule();
                mr.name = ca.Name;
                mr.value = ca.GetValue(t, null);
                modelRuleList.Add(mr);
            }
        }
    }

    public class DoValidate<T>
    {
        public bool DoVa(T t)
        {
            getModelRule<T> getModelRule = new getModelRule<T>(t);
            List<ModelRule> modelRuleList = getModelRule<T>.modelRuleList;
            var ModelRuleValidateRList = ModelRuleValidateR.modelRuleValidateList;

            bool r_bool = true;

            //x循环模型信息，从规则中找出对应的字段规则
            foreach (var ca in ModelRuleValidateRList)
            {
                //规则,
                var modelRule = modelRuleList.Where(x => x.name == ca.name).FirstOrDefault();
                if (modelRule != null)
                {
                    if (!DoVa(ca,modelRule))
                    {
                        r_bool = false;
                        break;
                    }
                }
            }

            return r_bool;
        }

        public bool DoVa(ModelRuleValidate modelRuleValidate, ModelRule modelRule)
        {
            bool IsOk = true;
            switch(modelRuleValidate.RuleName)
            {
                case "Require":
                    if (modelRule.value == null)
                    {
                        S_Result.error = modelRuleValidate.ErrorMessge;
                        IsOk= false;
                    }
                    else if (modelRule.value.ToString().Trim() == "")
                    {
                        S_Result.error = modelRuleValidate.ErrorMessge;
                        IsOk= false;
                    }
                    break;

                case "StringLenth":
                    if (modelRule.value != null)
                    {
                        if (modelRule.value.ToString().Length >Convert.ToInt32(modelRuleValidate.RuleVlues))
                        {
                            S_Result.error = modelRuleValidate.ErrorMessge;
                            return false;
                        }
                    }
                    
                    break;
            }
            return IsOk;

        }
    }


}
